package com.deco.back.controller.system;

import com.deco.back.utils.EncryptUtil;
import com.deco.dao.domain.table.CmsUser;
import com.deco.domain.vo.constants.CmsConstant;
import com.deco.domain.vo.sys.CmsUserVo;
import com.deco.service.sys.CmsRoleService;
import com.deco.service.sys.CmsUserService;
import com.free.common.vo.PageResponseVo;
import com.free.common.vo.PageSearchVo;
import com.free.common.vo.ResultModel;
import com.free.ext.web.spring.annoation.PageSearch;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
@Controller
public class CmsUserController {
    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/
    @Autowired
    private CmsUserService cmsUserService;

    @Autowired
    private CmsRoleService cmsRoleService;
    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    @RequestMapping(value="/cms_user_list.do")
    public ModelAndView list(){
        ModelAndView mv = new ModelAndView("system/cmsuserlist");
        return mv;
    }

    @RequestMapping(value="/cms_user_list_data.do")
    @ResponseBody
    public PageResponseVo<CmsUserVo> getList(@PageSearch PageSearchVo<CmsUserVo> condition){
        return cmsUserService.selectPageInfoByCmsUser(condition);
    }

    @RequestMapping(value = "/cms_user_edit.do")
    public ModelAndView update(Integer id){
        ModelAndView mv = new ModelAndView("system/cmsuseredit");
        CmsUserVo cmsUser;
        if(id == null){
            cmsUser = new CmsUserVo();
            mv.addObject("actNm","新增");
            mv.addObject("closeFlg","");
            cmsUser.setRolelist(cmsRoleService.getAll());
        }else{
            cmsUser = cmsUserService.getCmsUserById(id);
            mv.addObject("actNm","更新");
            mv.addObject("closeFlg","disabled");
            cmsUser.setRolelist(cmsRoleService.getActivedRoles(cmsUser.getRoles()));
        }
        mv.addObject("obj",cmsUser);
        return mv;
    }

    @RequestMapping(value = "/cms_user_add.do")
    @ResponseBody
    public ResultModel save(CmsUserVo vo){
        ResultModel res = cmsUserService.saveCheck(vo);
        if(!res.isSuccess()){
            return res;
        }
        vo.setPassword(EncryptUtil.encript(vo.getPassword(), EncryptUtil.CHARSET_UTF8));
        return cmsUserService.save(vo);
    }

    @RequestMapping(value = "/cms_user_edit_sub.do")
    @ResponseBody
    public ResultModel editSub(CmsUserVo vo){
        ResultModel res = cmsUserService.saveCheck(vo);
        if(!res.isSuccess()){
            return res;
        }
        if(StringUtils.isNotEmpty(vo.getPassword())){
            vo.setPassword(EncryptUtil.encript(vo.getPassword(), EncryptUtil.CHARSET_UTF8));
        }
        return cmsUserService.save(vo);
    }

    @RequestMapping(value = "/cms_user_del.do")
    @ResponseBody
    public ResultModel delete(CmsUserVo vo){
        return cmsUserService.delete(vo);
    }

    @RequestMapping(value = "/cms_user_modify_pwd.do")
    @ResponseBody
    public ResultModel udpSubmit(CmsUserVo vo){
        ResultModel res = cmsUserService.checkUpdateuserPwd(vo);
        if(res.isSuccess()){
            vo.setPassword(EncryptUtil.encript(vo.getPassword(), EncryptUtil.CHARSET_UTF8));
        }
        return cmsUserService.updateUserPwd(vo);
    }

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
